package com.chinatechstar.notification.controller;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chinatechstar.notification.entity.SysMsg;
import com.chinatechstar.notification.service.SysMsgService;
import com.chinatechstar.component.commons.result.ActionResult;
import com.chinatechstar.component.commons.result.ListResult;
import com.chinatechstar.component.commons.result.ResultBuilder;
import com.chinatechstar.component.commons.utils.ExcelUtils;
import com.chinatechstar.component.commons.validator.InsertValidator;
import com.chinatechstar.component.commons.validator.UpdateValidator;

/**
 * 消息管理 的控制层
 */
@Api(value = "消息管理 的控制层", tags = {"消息管理"})
@RestController
@RequestMapping("/sysmsg")
public class SysMsgController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private SysMsgService sysMsgService;

    /**
     * 查询 消息管理 所有数据
     *
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 消息管理 所有数据", tags = {"消息管理"})
    @GetMapping(path = "/findSysMsgAll")
    public ListResult<Object> findSysMsgAll() {
        List<SysMsg> list = sysMsgService.findSysMsgAll();
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 查询 消息管理 分页
     *
     * @param currentPage 当前页数
     * @param pageSize    每页记录数
     * @param name        名称
     * @param sorter      排序
     * @return ListResult<Object>
     */
    @ApiOperation(value = "查询 消息管理 分页", tags = {"消息管理"})
    @GetMapping(path = "/findSysMsgByCondition")
    public ListResult<Object> findSysMsgByCondition(@RequestParam(name = "currentPage", required = false, defaultValue = "1") Integer currentPage,
                                                    @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                    @RequestParam(name = "name", required = false) String name,
                                                    @RequestParam(name = "sorter", required = false) String sorter) {
        Map<String, Object> data = sysMsgService.findSysMsgByCondition(currentPage, pageSize, name, sorter);
        return ResultBuilder.buildListSuccess(data);
    }

    /**
     * 根据查询条件导出 消息管理
     *
     * @param response 响应对象
     * @param paramMap 参数Map
     */
    @ApiOperation(value = "根据查询条件导出 消息管理", tags = {"消息管理"})
    @PostMapping(path = "/exportSysMsg")
    public void exportSysMsg(HttpServletResponse response, @RequestParam Map<String, Object> paramMap) {
        try {
            List<String> headList = Arrays.asList("ID", "");
            List<LinkedHashMap<String, Object>> dataList = sysMsgService.findSysMsgForExcel(paramMap);
            ExcelUtils.exportExcel(headList, dataList, "导出信息", response);
        } catch (Exception e) {
            logger.warn(e.toString());
        }
    }

    /**
     * 根据主键ID查询 消息管理 数据
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数据查询 消息管理 数据", tags = {"消息管理"})
    @GetMapping(path = "/findSysMsgById")
    public ActionResult findSysMsgById(@RequestParam(name = "id") Long id) {
        SysMsg data = sysMsgService.findSysMsgById(id);
        return ResultBuilder.buildActionSuccess(data);
    }

    /**
     * 根据主键ID数组查询 消息管理 数据
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组查询 消息管理 数据", tags = {"消息管理"})
    @GetMapping(path = "/findSysMsgByIds")
    public ListResult<Object> findSysMsgByIds(@RequestParam(name = "ids") Long[] ids) {
        List<SysMsg> list = sysMsgService.findSysMsgByIds(ids);
        return ResultBuilder.buildListSuccess(list);
    }

    /**
     * 新增 消息管理
     *
     * @param sysMsg 消息管理 实体
     * @return ActionResult
     */
    @ApiOperation(value = "新增 消息管理", tags = {"消息管理"})
    @PostMapping(path = "/addSysMsg")
    public ActionResult addSysMsg(@Validated(InsertValidator.class) @RequestBody SysMsg sysMsg) {
        sysMsgService.insertSysMsg(sysMsg);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量新增 消息管理
     *
     * @param sysMsgs 消息管理 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量新增 消息管理", tags = {"消息管理"})
    @PostMapping(path = "/addSysMsgs")
    public ActionResult addSysMsgs(@Validated(InsertValidator.class) @RequestBody List<SysMsg> sysMsgs) {
        sysMsgService.insertSysMsgs(sysMsgs);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 编辑 消息管理
     *
     * @param sysMsg 消息管理 实体
     * @return ActionResult
     */
    @ApiOperation(value = "编辑 消息管理", tags = {"消息管理"})
    @PutMapping(path = "/updateSysMsg")
    public ActionResult updateSysMsg(@Validated(UpdateValidator.class) @RequestBody SysMsg sysMsg) {
        sysMsgService.updateSysMsg(sysMsg);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 批量编辑 消息管理
     *
     * @param sysMsgs 消息管理 实体数组
     * @return ActionResult
     */
    @ApiOperation(value = "批量编辑 消息管理", tags = {"消息管理"})
    @PostMapping(path = "/updateSysMsgs")
    public ActionResult updateSysMsgs(@Validated(UpdateValidator.class) @RequestBody List<SysMsg> sysMsgs) {
        sysMsgService.updateSysMsgs(sysMsgs);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 消息管理
     *
     * @param id ID
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID删除 消息管理", tags = {"消息管理"})
    @PostMapping(path = "/deleteSysMsgById")
    public ActionResult deleteSysMsgById(@RequestParam(name = "id") Long id) {
        sysMsgService.deleteSysMsgById(id);
        return ResultBuilder.buildActionSuccess();
    }

    /**
     * 删除 消息管理
     *
     * @param ids ID数组
     * @return ActionResult
     */
    @ApiOperation(value = "根据主键ID数组批量删除 消息管理", tags = {"消息管理"})
    @PostMapping(path = "/deleteSysMsgByIds")
    public ActionResult deleteSysMsgByIds(@RequestParam(name = "id") Long[] ids) {
        sysMsgService.deleteSysMsgByIds(ids);
        return ResultBuilder.buildActionSuccess();
    }

}
